#include "EditAccountDialog.h"
#include "ui_EditAccountDialog.h"

EditAccountDialog::EditAccountDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditAccountDialog)
{
    ui->setupUi(this);
}

EditAccountDialog::~EditAccountDialog()
{
    delete ui;
}

void EditAccountDialog::Edit(QString email)
{
    ui->EditAccountLabel->setText(email);

    if(!set.GetValueOfKey(QString("Account/").append(email).append("/password")).toString().isEmpty())
        ui->PasswordLineEdit->setText(set.DecryptPassword(email));

    if(set.GetValueOfKey(QString("Account/").append(email).append("/notifications")).toBool())
        ui->NotificationsCheckBox->setCheckState(Qt::Checked);
    else
        ui->NotificationsCheckBox->setCheckState(Qt::Unchecked);

    if(set.GetValueOfKey(QString("Account/").append(email).append("/password")).toBool())
        ui->PasswordCheckBox->setCheckState(Qt::Checked);
    else
        ui->PasswordCheckBox->setCheckState(Qt::Unchecked);

}

void EditAccountDialog::on_SaveButton_clicked()
{

    if(ui->NotificationsCheckBox->isChecked())
        set.SetValueForKey(QString("Account/").append(ui->EditAccountLabel->text()).append("/notifications"),true);
    else
        set.SetValueForKey(QString("Account/").append(ui->EditAccountLabel->text()).append("/notifications"),false);

    if(ui->PasswordCheckBox->isChecked())
    {
        if(!ui->PasswordLineEdit->text().isEmpty())
        {
            set.EncryptPassword(ui->EditAccountLabel->text(),ui->PasswordLineEdit->text());
            EditAccountDialog::accept();
        }
        else
        {
            ui->PasswordLineEdit->setStyleSheet("QLineEdit{border:1px solid red;}");
            ui->ErrorLabel->setText("Enter the password");
        }
    }
    else
    {
        set.SetValueForKey(QString("Account/").append(ui->EditAccountLabel->text()).append("/password"),"");
        EditAccountDialog::accept();
    }
}

void EditAccountDialog::on_BackButton_clicked()
{
    EditAccountDialog::close();
}

void EditAccountDialog::on_PasswordLineEdit_textChanged(const QString &arg1)
{
    ui->PasswordLineEdit->setStyleSheet("QLineEdit{border:1px solid gray;}");
}
